package logic

import (
	"base/baserpc/baserpc"
	"base/baserpc/internal/svc"
	"base/models/mysql"
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 笔记创建
func (l *AddLogic) Add(in *baserpc.AddRequest) (*baserpc.AddResponse, error) {
	// todo: add your logic here and delete this line
	note := mysql.Note{
		Title:   in.Title,
		Address: in.Address,
		NoteId:  int(in.Noteid),
	}
	mysql.DB.Debug().Find("note_id=?", in.Noteid).Find(&note)
	if note.ID != 0 {
		return nil, fmt.Errorf("用户已存在")
	}
	mysql.DB.Debug().Create(&note)
	return &baserpc.AddResponse{
		Id: int64(note.ID),
	}, nil
}
